package com.ruoyi.system.controlle;

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

@RestController
public class FileController {

    @GetMapping(value = "/files/display/{filename}", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<InputStreamResource> displayFileContent(@PathVariable String filename) {
        File file = new File("D:/user/" + filename);

        try {
            if (file.exists()) {
                FileInputStream fileInputStream = new FileInputStream(file);
                return ResponseEntity.ok()
                        .contentType(MediaType.TEXT_PLAIN)
                        .body(new InputStreamResource(fileInputStream));
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (IOException e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}